package cn.com.gridinfo.sooc.core.expand.signin.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;

/**
 * 历史签到界面的列表的适配器
 *
 * @author yingchunli
 */
public class HistorySignAdapter extends UltimateViewAdapter<HistorySignAdapter.ViewHolder> {

    public List<ClassInfo> classInfoList;
    Context context;

    public HistorySignAdapter(Context context, List<ClassInfo> classInfoList) {
        this.context = context;
        this.classInfoList = classInfoList;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.expand_sign_historysign_item, parent, false);

        return new ViewHolder(view, true);
    }

    @Override
    public int getAdapterItemCount() {
        return classInfoList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= classInfoList.size() : position < classInfoList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            ClassInfo classInfo = classInfoList.get(position);
            holder.kcmc.setText(classInfo.getKcmc());
            holder.qdri.setText(classInfo.getQdri());
            holder.lsmc.setText(classInfo.getLsmc());
            holder.ktzt.setText(classInfo.getKtzt());
            if (classInfo.getSkdd().isEmpty() || classInfo.getSkdd() == null) {
                //没有上课地点  不显示上课地点和icon
                holder.skdd.setVisibility(View.GONE);
            } else {
                //有上课地点
                holder.skdd.setText(classInfo.getSkdd());
                holder.skdd.setVisibility(View.VISIBLE);
            }
            if (classInfo.getQdms().equals("0")) {
                //该课堂不需要拍照签到  不显示拍照签到和icon
                holder.pzqd.setVisibility(View.GONE);
            } else {
                //该课堂需要拍照签到
                holder.pzqd.setVisibility(View.VISIBLE);
            }
            //底部分割线的显示
//            if (position == classInfoList.size() - 1) {
//                holder.line.setVisibility(View.VISIBLE);
//            } else {
//                holder.line.setVisibility(View.GONE);
//            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    public void loadmore(List<ClassInfo> list) {
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<ClassInfo> list) {
        clear(classInfoList);
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        TextView kcmc, lsmc, ktzt, qdri, skdd, pzqd;//课程名称，老师名称，课堂主题，签到日期，上课地点,拍照签到
        View line;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                kcmc = (TextView) itemView.findViewById(R.id.kcmc_historysign);//课程名称
                lsmc = (TextView) itemView.findViewById(R.id.lsmc_historysign);//老师名称
                ktzt = (TextView) itemView.findViewById(R.id.ktzt_historysign);//课堂主题
                qdri = (TextView) itemView.findViewById(R.id.qdri_historysign);//签到日期
                skdd = (TextView) itemView.findViewById(R.id.skdd_historysign);//上课地点
                pzqd = (TextView) itemView.findViewById(R.id.pzqd_historysign);//拍照签到
//                line = itemView.findViewById(R.id.line_bottom);//底部分割线
            }
        }
    }

}
